package com.hll.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hll.api.config.web.RespMessage;
import com.hll.api.entity.SouParm;
import com.hll.api.service.KaolaApi;
import com.hll.api.util.HttpUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * @author 74172
 * @create 2019-06-22 15:25
 */
@Service
public class KaolaApiImpl implements KaolaApi {

    @Value("${api.kaola.index}")
    String indexUrl;
    @Value("${api.kaola.index-top}")
    String indexTopUrl;
    @Value("${api.kaola.index-sou}")
    String indexSouUrl;
    @Value("${api.kaola.index-sou-tips}")
    String indexSouTipsUrl;

    @Value("${api.kaola.index-sou-goods}")
    String indexSouGoodsUrl;

    @Value("${api.kaola.goods-info}")
    String goodsInfoUrl;


    @Override
    public RespMessage getIndex(Integer pageNo) {
        try {
            Map<String,Object> map = new HashMap<>(2);
            map.put("t",System.currentTimeMillis());
            map.put("pageNo",pageNo<=0?1:pageNo);
            String ssl = HttpUtils.getSSL( indexUrl,map);
            if ("".equals(ssl)) {
                return RespMessage.error("网络超时，调用接口返回为空");
            }
            JSONObject json = JSON.parseObject(ssl);
            if (json.getBoolean("success")&&json.getIntValue("retCode")==0){
                return RespMessage.success(json.getJSONObject("data"),"成功");
            }else {
                return RespMessage.error(json.getString("desc"));
            }
        } catch (Exception e) {
            return RespMessage.error("服务器异常："+e.getMessage());
        }
    }

    @Override
    public RespMessage getIndexTop() {
        try {
            Map<String,Object> map = new HashMap<>(2);
            map.put("t",System.currentTimeMillis());
            map.put("location","https://m.kaola.com/");
            String ssl = HttpUtils.getSSL(indexTopUrl,map);
            if ("".equals(ssl)) {
                return RespMessage.error("网络超时，调用接口返回为空");
            }
            JSONObject json = JSON.parseObject(ssl);
            if (json.getBoolean("success")&&json.getIntValue("code")==200){
                return RespMessage.success(json.getJSONObject("data"),"成功");
            }else {
                return RespMessage.error(json.getString("desc"));
            }
        } catch (Exception e) {
            return RespMessage.error("服务器异常："+e.getMessage());
        }
    }

    @Override
    public RespMessage getIndexSou() {
        try {
            Map<String,Object> map = new HashMap<>(2);
            map.put("t",System.currentTimeMillis());
            String ssl = HttpUtils.getSSL(indexSouUrl,map);
            if ("".equals(ssl)) {
                return RespMessage.error("网络超时，调用接口返回为空");
            }
            JSONObject json = JSON.parseObject(ssl);
            if (json.getIntValue("code")==0){
                return RespMessage.success(json.getJSONObject("body").getJSONObject("result"),"成功");
            }else {
                return RespMessage.error(json.getString("msg"));
            }
        } catch (Exception e) {
            return RespMessage.error("服务器异常："+e.getMessage());
        }
    }

    @Override
    public RespMessage getIndexSouTips(String key) {
        try {
            Map<String,Object> map = new HashMap<>(2);
            map.put("t",System.currentTimeMillis());
            map.put("key",key);
            String ssl = HttpUtils.getSSL(indexSouTipsUrl,map);
            if ("".equals(ssl)) {
                return RespMessage.error("网络超时，调用接口返回为空");
            }
            JSONObject json = JSON.parseObject(ssl);
            if (json.getIntValue("code")==0){
                return RespMessage.success(json.getJSONObject("body"),"成功");
            }else {
                return RespMessage.error(json.getString("msg"));
            }
        } catch (Exception e) {
            return RespMessage.error("服务器异常："+e.getMessage());
        }
    }

    @Override
    public RespMessage getIndexSouGoods(SouParm parm) {
        try {

            String ssl = HttpUtils.postSSL(indexSouGoodsUrl,JSON.toJSONString(parm));
            if ("".equals(ssl)) {
                return RespMessage.error("网络超时，调用接口返回为空");
            }
            JSONObject json = JSON.parseObject(ssl);
            if (json.getIntValue("code")==200){
                return RespMessage.success(json.getJSONObject("body").getJSONObject("result"),"成功");
            }else {
                return RespMessage.error(json.getString("msg"));
            }
        } catch (Exception e) {
            return RespMessage.error("服务器异常："+e.getMessage());
        }
    }

    @Override
    public byte[] getImg(String imgCode, Integer width, Integer quality) {
        String url="https://pop.nosdn.127.net/"+imgCode+"?thumbnail="+width+"&quality="+quality;
        try {
            return HttpUtils.getSSLImg( url );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    @Override
    public RespMessage getGoodsInfo(Long goodsId) {
        try {
            Map<String,Object> map = new HashMap<>(2);
            map.put("t",System.currentTimeMillis());
            map.put("goodsId",goodsId);
            map.put("provinceCode",610000);
            map.put("cityCode",610100);
            map.put("districtCode",610102);
            String ssl = HttpUtils.getSSL(goodsInfoUrl,map);
            if ("".equals(ssl)) {
                return RespMessage.error("网络超时，调用接口返回为空");
            }
            JSONObject json = JSON.parseObject(ssl);
            if (json.getIntValue("code")==200){
                return RespMessage.success(json.getJSONObject("data"),"成功");
            }else {
                return RespMessage.error(json.getString("msg"));
            }
        } catch (Exception e) {
            return RespMessage.error("服务器异常："+e.getMessage());
        }
    }

    @Override
    public RespMessage getGoodsReviews(Long goodsId) {
        try {
            Map<String,Object> map = new HashMap<>(2);
            map.put("t",System.currentTimeMillis());
            map.put("goodsId",goodsId);
            String ssl = HttpUtils.getSSL("https://community.kaola.com/api/article/goods/detail/page/for/wap",map);
            if ("".equals(ssl)) {
                return RespMessage.error("网络超时，调用接口返回为空");
            }
            JSONObject json = JSON.parseObject(ssl);
            if (json.getIntValue("code")==0){
                return RespMessage.success(json.getJSONObject("body"),"成功");
            }else {
                return RespMessage.error(json.getString("msg"));
            }
        } catch (Exception e) {
            return RespMessage.error("服务器异常："+e.getMessage());
        }
    }


}
